<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<style>
.table tbody tr td{
            vertical-align: middle;
        }
        </style>
<section class="content">
        <div class="row">
            
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">订单资料</h3>                                
                    </div>
                    <div class="box-body">

                    <?php
                    if($checkcount>1):
                    ?>
            <div class="alert alert-warning alert-dismissible">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h4><i class="icon fa fa-warning"></i> 注意!</h4>
                该用户三天内共有<?php echo $checkcount;?>个订单，可以合并发货.
              </div>
                    <?php
                    endif;
                    ?>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>订单编号</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["ordersn"];?></span>
                            </div>
                        </div>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>订单总价</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;">$<?php echo $order["amount"];?></span>
                            </div>
                        </div>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>订单来源</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $site["name"];?></span>
                            </div>
                        </div>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>创建时间</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["addtime"];?></span>
                            </div>
                        </div>

                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>快递单号</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;">
                                <?php if(!empty($order["shipping"])): ?>
                                <?php
        $shiplist=explode(",",$order["shipping"]);
        foreach($shiplist as $shiprow)
        {
            if(substr($shiprow,0,2)=="SF")
            {
                $link="https://www.sf-express.com/us/en/dynamic_function/waybill/#search/bill-number/".$shiprow;
            }
            else
            {
                $link="https://global.cainiao.com/detail.htm?mailNoList=".$shiprow."&spm=a3708.7860688.0.d01&lang=en";
            }
            echo '<a href="'.$link.'" target="_blank">'.$shiprow.'</a>';
            echo '<a href=" https://t.17track.net/en#nums='.$shiprow.'" target="_blank">17track</a>';
        }
    ?>
                                <?php else:?>
                                尚未发货
                                <?php endif;?>

                                </span>
                            </div>
                        </div>

                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>备注</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["note"];?></span>
                            </div>
                        </div>

                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>订单状态</label>';?>
                            <div class="col-sm-7">
                            <span class="label <?php echo $statelabel[$order["state"]]; ?>"><?php echo $statetype[$order["state"]];?></span>
                             <?php

                                if($order["state"]=="1" || $order["state"]=="9")
                                {
                                    echo '<button class="btn btn-info btn-xs btn-flat" id="refreshbtn" itemid="'.$order["id"].'">手动刷新状态</button>';
                                }
                             ?>
                            </div>
                        </div>
                        <?php if($order["buy_source"]):?>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>采购来源</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["buy_source"];?></span>
                            </div>
                        </div>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>采购单号</label>';?>
                            <div class="col-sm-7" oid="<?php echo $order["id"];?>">
                                <span style="line-height:20px;" ondblclick="edit(this,'buy_ordersn')"><?php echo $order["buy_ordersn"];?></span>
                            </div>
                        </div>
                        <?php endif;?>
                        <?php if($order["buy_shipnum"]):?>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>采购物流</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["buy_shipnum"];?></span>
                            </div>
                        </div>
                        <?php endif;?>

                        <?php if($order["trackurl"]):?>
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>追踪物流</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><a href="<?php echo $order["trackurl"];?>" target="_blank"><?php echo $order["trackurl"];?></a></span>
                            </div>
                        </div>
                        <?php endif;?>


                    </div>

                    <div class="box-header with-border">
                        <h3 class="box-title">产品列表 <span class="btn btn-xs btn-success btn-flat" id="addproduct">添加产品</span></h3>                                
                    </div>
                    <div class="box-body">

                            <table class="table table-striped table-hover" style="margin-top:5px;">
                                <thead>
                                    <tr>
                                        <th class="text-center" width="100">照片</th>
                                        <th>名称</th>
                                        <th>变体</th>
                                        <th>数量</th>
                                        <th>价格</th>
                                        <th>产品来源</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach($order["productinfo"] as $pro){?>
                                        <tr>

                                            <?php 
                                            $tmp=explode(" - ",$pro["name"]);
                                            if(count($tmp)>1)
                                            {
                                                $qname=$tmp[0];
                                                $variant=str_replace($qname." - ","",$pro["name"]);
                                            }
                                            else
                                            {
                                                $qname=$pro["name"];
                                                $variant="";
                                            }
                                            //$qname=urlencode($qname);
                                            $siteprourl="/backend/sites/product/index/".$order["siteid"]."?keyword=".urlencode($qname);
                                            ?>
                                            <td class="imgcol">
                                            <?php if($pro["thumb"]){ ?>
                                            <img src="<?php echo $pro["thumb"];?>" height="80"/>
                                            <?php                                             
                                            }
                                            ?>
                                            </td>
                                            <td>    
                                            <a target="_blank" href="<?php echo isset($pro["siteurl"])?$pro["siteurl"]:"#";?>"><?php echo $qname;?></a></td>
                                            <td>
                                            <?php
                                                if(isset($pro["variant"]))
                                                {
                                                    echo $pro["variant"];
                                                }
                                                else
                                                {
                                                    echo $variant;
                                                }
                                            ?>                                            
                                            </td>
                                            <td><?php echo $pro["quantity"];?></td>
                                            <td>$ <?php echo sprintf("%.2f", $pro["price"]);?></td>
                                           
                                            <td><a class="btn btn-primary btn-xs btn-flat" target="_blank" href="<?php echo $pro["url"]&&$pro["url"]!="#"?$pro["url"]:"https://www.google.com/search?q=".urlencode($qname);?>">查看</a>&nbsp;<a class="btn btn-primary btn-xs btn-flat" target="_blank" href="<?php echo $siteprourl;?>">后台</a> <?php if(false && $pro["localid"]>0):?>&nbsp;<a class="btn btn-primary btn-xs btn-flat" target="_blank" href="/backend/products/editgoods/<?php echo $pro["localid"];?>">编辑</a><?php endif;?></td>
                                        </tr>
                                    <?php }?>
                                </tbody>
                            </table>
                    </div>

                    <div class="box-header with-border">
                        <h3 class="box-title">配送信息</h3>                                
                    </div>
                    <div class="box-body">
       

                        <div class="form-group col-sm-12">
                             <div class="col-sm-12">
                                <div style="line-height:20px;" oid="<?php echo $order["id"];?>">
                                <span ondblclick="edit(this,'realname')"><?php echo $order["realname"];?></span><br/>
                                <span ondblclick="edit(this,'address1')"><?php echo $order["address1"];?></span><br/>
                                <?php if($order["address2"]) echo '<span ondblclick="edit(this,\'address2\')">'.$order["address2"]."</span><br/>";?>
                                <span ondblclick="edit(this,'city')"><?php echo $order["city"];?></span> <span ondblclick="edit(this,'provineiso')"><?php echo $order["provineiso"];?></span> <span ondblclick="edit(this,'zipcode')"><?php echo $order["zipcode"];?></span><br/>
                                <span ondblclick="edit(this,'countryiso')"><?php echo $order["countryiso"];?></span><br/>
                                <span ondblclick="edit(this,'phone')"><?php echo $order["phone"];?></span>
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="box-header with-border">
                        <h3 class="box-title">买家信息</h3>                                
                    </div>
                    <div class="box-body">
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>邮箱</label>';?>
                            <div class="col-sm-7" oid="<?php echo $order["id"];?>">
                                <span style="line-height:20px;" ondblclick="edit(this,'email')"><?php echo $order["email"];?></span>
                            </div>
                        </div>
                       
                        <div class="form-group col-sm-4">
                            <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-5 control-label')).'>付款方式</label>';?>
                            <div class="col-sm-7">
                                <span style="line-height:20px;"><?php echo $order["extra"];?></span>
                            </div>
                        </div>
                       

                        <div class="form-group">
                            <div class="col-sm-12">
                                <div class="btn-group">
                                    
                                    <?php
                                    if(in_array($order["state"],[1,2,3,8])):
                                    ?>
                                    <button class='btn btn-warning btn-flat' id="cancelbtn" itemid="<?php echo $order["id"];?>">取消订单</button>
                                    <?php
                                    endif;
                                    ?>
                                </div>
                                

                                <?php
                                    if(in_array($order["state"],[3,4])):
                                    ?>
                                     <div class="btn-group">
                                    <button class='btn btn-info btn-flat' id="resetbtn" itemid="<?php echo $order["id"];?>">重置采购发货</button>
                                    </div>
                                    <?php
                                    endif;
                                    ?>

                                <?php if(in_array($order["state"],[2,3]) && $config["shiptype"]==1){ ?>
                   
                   <div class="btn-group">
                       <a href="/backend/sfexpress/index/<?php echo $order["id"];?>" class='btn btn-success btn-flat' target="_top">在线发送快递</a>
                   </div>
         
           <?php } ?>
                            </div>
                        </div>    


                        
                    </div>
                                     
                </div>
            </div>
        </div>
</section>

<script>

atedit=0;
function edit(obj,attrName){
    if (atedit==1)
    return;
    else
    atedit=1;

    var oldHtml = obj.innerHTML;
    var newobj = document.createElement('input');
    newobj.name = attrName;
    newobj.type = "text";
    newobj.setAttribute("class","form-control input-sm");
    if($.trim(oldHtml)!="双击设置"){
        newobj.value = oldHtml;
    }

    var cid = $(obj).parent().attr("oid");     
    obj.innerHTML = '';  
    obj.appendChild(newobj);
    newobj.focus(); 

    $(newobj).keydown(function (event) {
        if (event.keyCode == 13) {
            $(this).trigger("blur");
        }
    });

    eurl="/backend/sites/order/newajaxedit";

    $(newobj).blur(function(){
        atedit=0;
        obj.innerHTML = this.value?this.value:"双击设置";       
        if(oldHtml != this.value){
            $.ajax({
                type:"post",
                url:eurl,
                dataType:"json",
                data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                error:function(){
                },
                success:function(){
                    layer.msg("更新成功");
                },
            });
        }  

    })
}


$(function(){

    var orderid=<?php echo $order["id"];?>;
    $("span#addproduct").on("click",function(){
        layer.prompt({
            formType: 0,
            placeholder: '必须是本店铺商品',
            title: '请输入商品名称',

        }, function(value, index, elem){
            if(value.length==0)
            {
                layer.msg("名称不能为空");
            }
            else
            {
                $.ajax({

                    type:"post",
                    url:"/backend/sites/order/addproduct/"+orderid,
                    dataType:"json",
                    data:{"pname":value},
                    error:function(){
                        layer.closeAll();
                    },
                    success:function(result){
                        layer.closeAll();
                        if(result["status"]==1)
                        {
                            window.location.href=window.location.href;
                            //layer.msg("订单已更新，请关闭后重新打开");
                        }
                        else
                        {
                            layer.msg(result["msg"]);
                        }
                        
                    },

                });

            }
        });
    });
    requirejs.config({
        paths: {
            jquery: "../../components/jquery/jquery.min",
            viewer: "../../components/viewer/viewer.min",
        },
        shim: {
            viewer: {deps: ["loadcss!../../components/viewer/viewer.min.css"]},        
        }
    });
    require(['jquery','viewer'], function($){
        $('.imgcol img').viewer({});
    });


    $("#refreshbtn").on("click",function(){
        var oid=$(this).attr("itemid");
        shadow = layer.load(1, {
            shade: [0.1,'#000'] 
        });
        $.ajax({
            type:"post",
            url:"/backend/sites/order/refresh/"+oid,
            dataType:"json",
            data:{},
            error:function(){
                layer.close(shadow);
            },
            success:function(result){
                if(result["status"]==1)
                {
                    layer.msg("订单已更新，请关闭后重新打开");
                }
                else
                {
                    layer.msg(result["msg"]);
                }
                layer.close(shadow);
            },
        });
    });

    $("#resetbtn").on("click",function(){
        var oid=$(this).attr("itemid");
        layer.confirm("确认要重置订单吗，重置后采购和发货信息将无法恢复？", { title: "确认" }, function (index) {             
            shadow = layer.load(1, {
                shade: [0.1,'#000'] 
            });
            $.ajax({
                type:"post",
                url:"/backend/sites/order/reset/"+oid,
                dataType:"json",
                data:{},
                error:function(){
                    layer.close(shadow);
                },
                success:function(result){
                    if(result["status"]==1)
                    {
                        layer.msg("订单已更新，请关闭后重新打开");
                    }
                    else
                    {
                        layer.msg(result["msg"]);
                    }
                    layer.close(shadow);
                },
            });
            layer.close(index);
        });
        
    });


    $("#cancelbtn").on("click",function(){
        var oid=$(this).attr("itemid");
        layer.confirm("确认要取消订单吗，取消后将无法恢复？", { title: "确认" }, function (index) {             
            shadow = layer.load(1, {
                shade: [0.1,'#000'] 
            });
            $.ajax({
                type:"post",
                url:"/backend/sites/order/cancel/"+oid,
                dataType:"json",
                data:{},
                error:function(){
                    layer.close(shadow);
                },
                success:function(result){
                    if(result["status"]==1)
                    {
                        layer.msg("订单已更新，请关闭后重新打开");
                    }
                    else
                    {
                        layer.msg(result["msg"]);
                    }
                    layer.close(shadow);
                },
            });
            layer.close(index);
        });
        
    });
});
</script>
